package summit.tests
{
import flash.errors.IllegalOperationError;
import flash.events.ErrorEvent;
import flash.events.Event;

import summit.basecamp.constants.CategoryFilter;
import summit.basecamp.helpers.CategoryHelper;
import summit.basecamp.helpers.MessageHelper;
import summit.data.AccountToken;
import summit.events.BasecampAPIEvent;
import summit.tests.tools.SummitTestContainer;
import summit.tests.tools.TestHelpers;

public class MessageHelperTests extends SummitTestContainer
{
	private var _messageId:String;
	private var _categoryId:String;
	private var _secondChange:Boolean = false;
	private var _createOnInit:Boolean = false;
	private var _prepareOnInit:Boolean = false;
	private var _fromTest:Boolean = false;
	
	private var _firstList:Boolean = true;
	
	public function MessageHelperTests(token:AccountToken)
	{
		super(token);
	}
	
	override protected function self_CompleteInit(e:Event):void
	{
		super.initializeLoader(loader_OKList);
		loader.load(MessageHelper.list(projectId));
	}
	
	override public function cleanUp():void
	{
		if(!_createOnInit) super.cleanUp();
		else 
		{
			super.initializeLoader(loader_OKCleanup);
			loader.load(MessageHelper.destroy(_messageId));
		}
	}
	
	private function loader_OKCleanup(e:BasecampAPIEvent):void
	{
		super.cleanUp();
	}
	
	public function readTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MessageHelper.read(_messageId));
	}
	
	public function prepareCreateDeleteTest(context:Object):void
	{
		if(!_createOnInit)
		{
			_fromTest = true;
			doPrepare();
		}
		else
		{
			trace("** prepare, create called on init - delete will come " + 
					"with cleanup.");
			super.completeAsyncTest();
		}
	}
	
	public function listTest(context:Object):void
	{
		super.initializeLoader(loader_OKListTest);
		loader.load(MessageHelper.list(projectId, _categoryId));	
	}
	
	private function loader_OKListTest(e:BasecampAPIEvent):void
	{
		if(_firstList)
		{
			_firstList = false;
			loader.load(MessageHelper.list(projectId, null, true));
		}
		else
		{
			super.initializeLoader(super.loader_OKComplete);
			loader.load(MessageHelper.list(projectId, _categoryId, true));
		}
	}
	
	public function editUpdateTest(context:Object):void
	{
		super.initializeLoader(loader_OKEdit);
		loader.load(MessageHelper.edit(_messageId));	
	}
	
	private function loader_OKEdit(e:BasecampAPIEvent):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MessageHelper.update(e.data));
	}
	
	private function loader_OKList(e:BasecampAPIEvent):void
	{
		if(0 != e.data.children().length())
		{
			_messageId = e.data.children()[0].id;	
			
			super.initializeLoader(loader_OKCategoryList);
			loader.load(CategoryHelper.list(projectId, CategoryFilter.POST));
		}
		else
		{
			if(!_secondChange)
			{
				_secondChange = true;
			}
			else 
			{
				throw new IllegalOperationError(
				"Attempt to create message failed.");
			
				super.dispatchErrorEvent(new ErrorEvent(ErrorEvent.ERROR, 
				false, false, 
				"Could not create message for message helper tests", -1));
				
				return;
			}
			
			// oh man.
			doPrepare();
		}
	}
	
	private function doPrepare():void
	{
		super.initializeLoader(loader_OKPrepare);
		loader.load(MessageHelper.prepare(projectId));
	}
	
	private function loader_OKCategoryList(e:BasecampAPIEvent):void
	{
		_categoryId = e.data.children()[0].id;
		super.self_CompleteInit(null);
	}
	
	private function loader_OKPrepare(e:BasecampAPIEvent):void
	{
		if(!_fromTest)
		{
			_prepareOnInit = true;
		}
		
		var msgXML:XML = prepareMessageForCreate(e.data);
		super.initializeLoader(loader_OKCreate);
		loader.load(MessageHelper.create(msgXML));
	}
	
	private function loader_OKCreate(e:BasecampAPIEvent):void
	{
		if(!_fromTest)
		{
			_createOnInit = true;
			self_CompleteInit(null);
		}
		else
		{
			super.initializeLoader(super.loader_OKComplete);
			loader.load(MessageHelper.destroy(
			TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders)));
		}
	}
	
	public static function prepareMessageForCreate(xml:XML):XML
	{
		var createXML:XML = new XML("<request/>");
		createXML.appendChild(xml);
		createXML.post.title = "Test Title";
		
		return createXML;
	}
	

}
}